package com.tanhua.manager.controller;

import com.tanhua.domain.db.Admin;
import com.tanhua.domain.vo.AdminVo;
import com.tanhua.manager.interceptors.AdminHolder;
import com.tanhua.manager.service.AdminService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("/system/users")
public class SystemUsersController {


    @Autowired
    private AdminService adminService;

//    获取验证码图片
//    请求方式 get
//     请求地址  /system/users/verification
//    请求参数  Query参数 uuid
//      返回类型  一张图片
    @GetMapping(value = "/verification",name = "获取验证码图片")
    public ResponseEntity verification(String uuid, HttpServletResponse response) throws Exception{
        return adminService.verification(uuid,response);
    }

 //    执行登录
//    请求方式 Post
//     请求地址  /system/users/login
//    请求参数  Body {username:xxx,password:12121,verificationCode:123456,uuid:sfgerwesdfbdr}
//      返回类型  {token:13erfwefsdcsdvcs}
    @PostMapping(value = "/login",name = "执行登录")
    public ResponseEntity login(@RequestBody Map param) throws Exception{
        String username = (String) param.get("username");
        String password = (String) param.get("password");
        String verificationCode = (String) param.get("verificationCode");
        String uuid = (String) param.get("uuid");
        return adminService.login(username,password,verificationCode,uuid);
    }
//    首页获取登录人信息
//    请求方式 Post
//     请求地址  /system/users/profile
//    请求参数 token
//      返回类型  AdminVo
    @PostMapping(value = "/profile",name = "首页获取登录人信息")
    public ResponseEntity profile() throws Exception{
        Admin admin = AdminHolder.getAdmin();
        AdminVo adminVo = new AdminVo();
        BeanUtils.copyProperties(admin,adminVo);

        //给token续命
        adminService.saveTokenToRedis(admin);

        return ResponseEntity.ok(adminVo);
    }

//    用户退出
//    请求方式 Post
//     请求地址  /system/users/logout
//    请求参数 token
//      返回类型
    @PostMapping(value = "/logout",name = "用户退出")
    public ResponseEntity logout() throws Exception{
        return adminService.logout();
    }
}
